package com.example.yuanqin

import android.util.Log
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.yuanqin.R
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

sealed class SplashEvent(){
    object JumpMainPage : SplashEvent()
}

@Preview
@Composable
fun splashPre(){
    MaterialTheme(colors = MaterialTheme.colors,
        ) {
        splash(5) {
            Log.e("TAG", "onCreate: jump",)
        }
    }
}


@Composable
fun splash(waitTime: Int,  onEvent: (SplashEvent) -> Unit) {
    val downTime = remember { mutableStateOf<Int>(waitTime)}
    Box{
        rememberCoroutineScope().launch {
            while (downTime.value-- > 1){
                delay(1000)
            }
            onEvent(SplashEvent.JumpMainPage)
        }
        Image(
            painter = painterResource(id = R.mipmap.splash),
            contentDescription = null,
            modifier = Modifier.fillMaxWidth().fillMaxHeight(),
            contentScale = ContentScale.Crop
        )
        TextButton(onClick = {onEvent(SplashEvent.JumpMainPage)},
            modifier = Modifier
                .absoluteOffset(310.dp, 25.dp)
                .background(
                    Color.Gray,
                    shape = RoundedCornerShape(4.dp)
                )
                .alpha(1.0f),
                border = BorderStroke(1.dp, Color.LightGray)
        ) {
            Text(
                text = "${downTime.value}秒",
                fontSize = 15.sp,
                color = Color.White,
                textAlign = TextAlign.Center,
            )
        }
    }
}
